﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Streambolics.Hp41s
{
    /// <summary>
    ///     An execution context.
    /// </summary>
    /// <remarks><para>
    ///     The execution context indicates how the machine executes programs. Different execution
    ///     contexts are used for the interactive mode, running mode, learn mode, etc.
    /// </para><para>
    ///     Contexts are stacked so that the currently executing instruction can be retrieved. This
    ///     also serves as the return stack.
    /// </para></remarks>

    public abstract class Context
    {
        private Context _Parent;
        private Engine _Machine;

        protected Context (Context aParent)
        {
            _Parent = aParent;
            _Machine = aParent.Machine;
            _Machine.PushContext (this);
        }

        protected Context (Engine aMachine)
        {
            _Parent = null;
            _Machine = aMachine;
            _Machine.PushContext (this);
        }

        public Context Parent
        {
            get
            {
                return _Parent;
            }
        }

        /// <summary>
        ///     The machine associated with the context.
        /// </summary>

        public Engine Machine
        {
            get
            {
                return _Machine;
            }
        }

        /// <summary>
        ///     The registers of the machine associated with the context.
        /// </summary>

        public Registers Registers
        {
            get
            {
                return _Machine.Registers;
            }
        }

        /// <summary>
        ///     The full long listing of the currently executing or paused
        ///     program.
        /// </summary>
        /// <returns>
        ///     A string containing the program listing in "full" mode.
        /// </returns>

        public virtual string LongListing ()
        {
            return String.Empty;
        }

        /// <summary>
        ///     Execute the given instruction.
        /// </summary>
        /// <param name="aInstruction">
        ///     The instruction to execute.
        /// </param>
        /// <remarks><para>
        ///     The normal thing to do is to call the Execute method of the instruction.
        ///     However, special contexts (such as the Learn Mode context) may do something
        ///     else, for example inserting the instruction in the current program.
        /// </para></remarks>

        protected internal abstract void Execute (Instruction aInstruction);

        /// <summary>
        ///     Execute follow-up instructions.
        /// </summary>
        /// <returns>
        ///     True if more instructions follow, false otherwise.
        /// </returns>

        protected internal virtual bool Run ()
        {
            return false;
        }

        /// <summary>
        ///     Process a context switch in the machine.
        /// </summary>
        /// <remarks><para>
        ///     This function is called whenever the current context
        ///     changes in the machine. This usually means that the Run()
        ///     method should exit, because a new context is now in control.
        /// </para></remarks>

        protected internal virtual void ContextSwitch ()
        {
        }

        /// <summary>
        ///     Implement a return instruction.
        /// </summary>

        public virtual void DoReturn ()
        {
            throw new NotImplementedException ();
        }

        /// <summary>
        ///     Execute a XEQ instruction
        /// </summary>
        /// <param name="aProgram"></param>
        /// <param name="aProgramCounter"></param>

        public virtual void DoXeq (Steps aProgram, int aProgramCounter)
        {
            new RunningContext (this, aProgram, aProgramCounter);
        }

        public virtual void DoGto (Steps aProgram, int aProgramCounter)
        {
        }

        public virtual void DoPrompt (string aDisplay)
        {
            _Machine.Display = aDisplay;
        }

        /// <summary>
        ///     Resume the execution.
        /// </summary>

        public virtual void DoResume ()
        {
            throw new NotImplementedException ();
        }

        public virtual void DoCondition (bool aCondition)
        {
            throw new NotImplementedException ();
        }

        /// <summary>
        ///     The string that should be displayed on the machine screen.
        /// </summary>
        /// <returns>
        ///     A string to be displayed.
        /// </returns>

        internal protected virtual string GetDisplay ()
        {
            //  TODO : Optimize when alpha mode, so that a Value is not created.

            return _Machine.Present (GetDisplayValue());
        }

        internal Value GetDisplayValue ()
        {
            // TODO : In Alpha mode, return the value of the Alpha register.
            return _Machine.Registers.X;
        }

        /// <summary>
        ///     Interpret a key click.
        /// </summary>
        /// <param name="aKey">
        ///     The key being clicked
        /// </param>

        public virtual void KeyClick (int aKey)
        {
        }
    }
}
